<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    top="8vh"
    :close-on-click-modal="false"
    @close="closeDialog"
    @click.native="closeDeptsBox"
  >
    <!-- 表单 -->
    <!-- 表单校验四要素
      1.el-form :model :rules
      2.el-form-item prop
      3.表单元素的 el-model
    -->
    <el-form ref="refForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="form.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="form.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="form.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in formOfEmploymentList"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="form.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- readonly disabled -->
        <el-input
          :value="form.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @click.native.stop="getDepartmentsList"
        />
        <div v-if="isShowTree" class="tree-box">
          <el-tree
            v-loading="isLoading"
            :data="deptsList"
            :props="{ label: 'name' }"
            @node-click="handleNodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confirmAddEmployee">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentsListApi } from '@/api/departments'
import { addEmployeeApi } from '@/api/empolyees'
import { transListToTreeData } from '@/utils'
import enumObj from '@/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户名不可为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户名长度为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不可为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不可为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '工号不可为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不可为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不可为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不可为空', trigger: 'blur' }
        ]
      },
      deptsList: [],
      isShowTree: false,
      isLoading: false,
      formOfEmploymentList: enumObj.hireType
    }
  },
  methods: {
    /**
     * 关闭弹框
     */
    closeDialog() {
      // 通知父组件 去修改showDialog的值
      // 1.子组件触发自定义事件来通知父组件
      // 2.子组件在内部修改父组件传过来的值
      this.$emit('update:showDialog', false)
      // 清空的时候 清空的 数据+校验状态
      // resetFields 清空的绑定了prop那些表单域的数据
      this.$refs.refForm.resetFields()
    },
    /**
     * 获取部门列表
     */
    async getDepartmentsList() {
      this.isShowTree = !this.isShowTree
      if (!this.isShowTree) return
      this.isLoading = true
      const {
        data: { depts }
      } = await getDepartmentsListApi()
      this.deptsList = transListToTreeData(depts, '')
      this.isLoading = false
    },
    /**
     * el-dialog的点击事件
     */
    closeDeptsBox() {
      this.isShowTree = false
    },
    /**
     * 点击树状结构
     */
    handleNodeClick(data) {
      if (data.children && data.children.length > 0) {
        // 点击的是非叶子节点（有子级）
        return
      }
      this.form.departmentName = data.name
      // 如果后端需要id的话 这么传
      // this.form.departmentid = data.id
      this.isShowTree = false
    },
    /**
     * 添加员工
     */
    confirmAddEmployee() {
      // 统一校验
      this.$refs.refForm.validate(async flag => {
        if (!flag) return
        await addEmployeeApi(this.form)
        this.$message.success('恭喜！添加成功')
        this.closeDialog()
        this.$parent.getEmployeesList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
